package com.xujianlun.cpms.common;

import java.io.Serializable;

public class Response<T> implements Serializable {

    private static final int SUCCESS_CODE = 200;
    private static final int ERROR_CODE = 0;

    private int code;
    private String msg;
    private T data;

    private Response(int code,String msg,T data){
        this.code=code;
        this.msg=msg;
        this.data=data;
    }

    private Response(int code,String msg){
        this.code=code;
        this.msg=msg;
    }

    public static <T> Response<T> success(String msg, T data){
        return new Response<T>(SUCCESS_CODE,msg,data);
    }

    public static <T> Response<T> success(){
        return new Response<T>(SUCCESS_CODE,"success");
    }

    public static <T> Response<T> success(T data){
        return new Response<T>(SUCCESS_CODE,"success",data);
    }

    public static <T> Response<T> error(){
        return new Response<T>(ERROR_CODE,"error");
    }

    public static <T> Response<T> error(String msg){
        return new Response<T>(ERROR_CODE,msg);
    }

    public static <T> Response<T> error(int code, String msg){
        return new Response<T>(code,msg);
    }

    public static <T> Response<T> error(int code, String msg, T data){
        return new Response<T>(ERROR_CODE,msg,data);
    }

    public int getCode(){
        return code;
    }

    public String getMessage(){
        return msg;
    }
    public T getData(){
        return data;
    }
}
